/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.comandos.adrs;

import br.ufc.quixada.adrs.interfaces.Comando;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.model.Prazo;
import br.ufc.quixada.adrs.service.PrazoService;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Escritorio projetos
 */
public class CmdListarPrazosAdrs implements Comando {

    public String executa(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdfAno = new SimpleDateFormat("MM/yyyy");
        Date dataAtual = new Date();
        HttpSession session = request.getSession();
        Adrs adrs = (Adrs) session.getAttribute("user");
        PrazoService ps = new PrazoService();

        List<Prazo> prazosAdrs = ps.getPrazosByAdrs(adrs);
        for (int i = 0; i < prazosAdrs.size(); i++) {
            Prazo p = prazosAdrs.get(i);
            if (!sdfAno.format(dataAtual).equals(sdfAno.format(p.getFim()))
                    && !sdfAno.format(dataAtual).equals(sdfAno.format(p.getInicio()))
                    && (dataAtual.after(p.getFim()) || dataAtual.before(p.getInicio()))) {
                prazosAdrs.remove(p);
                i--;
            }
        }

        Map<Prazo, Boolean> mapaDeConfirmacoes = new HashMap<Prazo, Boolean>();
        for (Prazo p : prazosAdrs) {
            mapaDeConfirmacoes.put(p, ps.isConfirmado(p, adrs));
        }

        session.setAttribute("prazosAdrs", mapaDeConfirmacoes);
        return "/adrs/adrs_prazos_agendados.jsp";
    }
}
